package com.libraries.image.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.libraries.helper.ImgHelper;
import com.libraries.image.activity.ImagePreviewActivity;
import com.libraries.image.entities.PreviewInfo;
import com.example.libraries.R;
import com.facebook.drawee.view.SimpleDraweeView;

@SuppressLint("NewApi")
public class ImageSelectAdapter extends BaseAdapter {
	private HashMap<Integer, Boolean> checkedMap;
	private ArrayList<String> lImages;
	private LayoutInflater mInflater;
	private Context context;
	@SuppressLint("UseSparseArrays")
	public ImageSelectAdapter(Context context, ArrayList<String> lImages) {
		this.context=context;
		this.lImages = lImages;
		this.mInflater = LayoutInflater.from(context);
		this.checkedMap = new HashMap<Integer, Boolean>();
	}
	
	public void notifyDataSetChanged(ArrayList<String> lImages){
		this.lImages=lImages;
		notifyDataSetChanged();
	}

	public ArrayList<String> getSelectedItems() {
		ArrayList<String> lSelected = new ArrayList<String>();
		Iterator<Map.Entry<Integer, Boolean>> iterator = (Iterator<Entry<Integer, Boolean>>) checkedMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<Integer,Boolean> entry = (Map.Entry<Integer,Boolean>) iterator.next();
			Integer key=entry.getKey();
//			Logger.log(this, "getSelectedItems", key);
			lSelected.add(lImages.get(key));
		}
		return lSelected;
	}

	@Override
	public int getCount() {
		return lImages.size();
	}

	@Override
	public Object getItem(int position) {
		return lImages.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.image_item, null);
			viewHolder.v_imgView = (SimpleDraweeView) convertView.findViewById(R.id.v_img);
			viewHolder.cb_select = (CheckBox) convertView.findViewById(R.id.cb_select);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String imgPath = (String) getItem(position);
		viewHolder.cb_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// 如果是未选中的CheckBox,则添加动画
				if ( !checkedMap.containsKey(position) ) {
					addAnimation(viewHolder.cb_select);
					checkedMap.put(position, isChecked);
				}
				else{
					checkedMap.remove(position);
				}
			}
		});

		viewHolder.cb_select.setChecked(checkedMap.containsKey(position) ? checkedMap
				.get(position) : false);
		viewHolder.v_imgView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String imgPath = (String) getItem(position);
				Intent intent = new Intent(context, ImagePreviewActivity.class);
				PreviewInfo previewInfo=new PreviewInfo();
				previewInfo.setImgPath(imgPath);
				previewInfo.setImgWidth(v.getWidth());
				previewInfo.setImgHeight(v.getHeight());
				int[] location = new int[2];
				v.getLocationOnScreen(location);
				previewInfo.setLocationX(location[0]);
				previewInfo.setLocationY(location[1]);
				intent.putExtra("previewInfo", previewInfo);
				context.startActivity(intent);
				((Activity) context).overridePendingTransition(0, 0);
			}
		});
		
		ImgHelper.loadFileByFresco(viewHolder.v_imgView, imgPath, null);
		return convertView;
	}

	/**
	 * 给CheckBox加点击动画
	 * 
	 * @param view
	 */
	private void addAnimation(View view) {
		float[] vaules = new float[] { 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f,
				1.1f, 1.2f, 1.3f, 1.25f, 1.2f, 1.15f, 1.1f, 1.0f };
		AnimatorSet set = new AnimatorSet();
		set.playTogether(ObjectAnimator.ofFloat(view, "scaleX", vaules),
				ObjectAnimator.ofFloat(view, "scaleY", vaules));
		set.setDuration(150);
		set.start();
	}

	public class ViewHolder {
		public SimpleDraweeView v_imgView;
		public CheckBox cb_select;
	}

}
